<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 通道签名
 * @package app\admin\model
 */
class BusPlatSign extends Model
{
    public function getList($request)
    {
        $where = [];

        if($request['plat_id'])
        {
            $where[] = ['bus_plat_sign.plat_id','=',$request['plat_id']];
        }

        $getData = $this
                ->field('bus_plat_sign.*,bus_sms_sign.sms_sign,bus_platfrom.platfrom')
                ->join('bus_platfrom','bus_platfrom.plat_id = bus_plat_sign.plat_id')
                ->join('bus_sms_sign','bus_sms_sign.id = bus_plat_sign.sms_sign_id')
                ->where($where)
                ->order('id desc')
                ->paginate($request['limit'], false, ['query'=>$request])
                ->each(function($item, $key){

                    $item->platfrom = $item->plat_id . '/' . $item->platfrom;

                    switch ($item->onoff) {
                        case 0:
                            $item->onoff = '<span class="layui-badge layui-bg-orange">已禁用</span>';
                            $item->onoff_btn = '<a class="classbtn" title="开" lay-event="start" style="background:#009688"><i class="layui-icon layui-icon-play"></i></a>';
                            break;
                        case 1:
                            $item->onoff = '<span class="layui-badge layui-bg-green">已开启</span>';
                            $item->onoff_btn = '<a class="classbtn" title="关" lay-event="stop" style="background:#FF5722"><i class="layui-icon layui-icon-pause"></i></a>';
                            break;

                        default:
                            $item->onoff = '';
                            $item->onoff_btn = '';
                            break;
                    }
                })->toarray();

        return $getData;
    }

    /**
     * 添加
     * @Author   lf
     * @DateTime 2019-09-23T15:52:19+0800
     */
    public function addOne($request)
    {
        $returnMsg = ['code' => 0 , 'msg' => '添加失败' ] ;

        try {
            // 步骤一
            // 检查签名库是否存在签名
            $signId = Db::name('bus_sms_sign')->where('sms_sign',$request['sms_sign'])->value('id');
            // 步骤二如果有就保存ID
            // 如果没有就新增签名，并且保存ID
            if(!$signId)
            {
                $data = ['sms_sign' => $request['sms_sign'], 'onoff' => '1'];
                $signId = Db::name('bus_sms_sign')->insertGetId($data);
            }

            if(BusPlatSign::get(['plat_id' => $request['plat_id'] , 'sms_sign_id' => $signId ]))
            {
                return ['code' => 0 , 'msg' => '该通道已经有相同的签名' ] ;
            }
            //保存
            $info              = new BusPlatSign;
            $info->plat_id     = $request['plat_id'];
            $info->sms_sign_id = $signId;
            $info->onoff       = $request['onoff'];
            $reslut            = $info->save();

            if($reslut)
            {
                trace(session('username').'添加通道签名'.$request['plat_id'].$request['sms_sign'].'成功');

                $returnMsg = ['code' => 200 , 'msg' => '成功' ];
            }else{
                trace(session('username').'添加通道签名'.$request['plat_id'].$request['sms_sign'].'失败');
            }

        } catch (\Exception $e) {

            trace(session('username').'添加通道签名'.$request['plat_id'].$request['sms_sign'].'失败'.$e->getMessage(),'error');

        }

        deleteCache();

        return $returnMsg;
    }
}